const {Craw, rize} = require('../app')
const req = new Craw({url: 'https://lists.meizu.com/page/list/?categoryid=231&click=gw_index_cdh_5'});

async function electronics() {
    await req.sleep(1000);
    await rize.click('#slCategory > div > div.mod-value > div > ul > li:nth-child(1) > a');
    await req.sleep(500);
    await rize.click('#slProp > div > div.mod-value > div > ul > li.vm-all > a');
    let arr_1 = await Promise.resolve(electronicsData())
    await rize.click('#pages > div > a.p-next.p-elem.iconfont');
    let arr_2 = await Promise.resolve(electronicsData())
    await rize.click('#pages > div > a.p-next.p-elem.iconfont');
    let arr_3 = await Promise.resolve(electronicsData())
    await rize.browser.close()
    console.log('获取数据完毕 返回Promise');
    return Promise.resolve([...arr_1,...arr_2,...arr_3]);
}
electronics()

async function electronicsData() {
    let data = await req.PageDown({
        fnc: function () {
            let li = document.querySelectorAll('#goodsListWrap > li > a > div');
            let arr = [];
            [].forEach.call(li, (item, index) => {
                let imgUrl = item.querySelector('.mod-pic img').src.replace(/@.+$/g, '');

                let img = [].map.call(item.querySelectorAll('.item-slide img'), (dom, num) => {
                    let i = num;
                    let obj = {};
                    obj[`imgWrap_${i}`] = dom.src.replace(/@.+$/g, '');
                    return obj
                })

                let DeviceNmae = item.querySelector('h2').innerText;
                let describe = item.querySelector('h3').innerText;
                let price = item.querySelector('.vm-price').innerText;

                arr.push({imgUrl, imgWrap: img, DeviceNmae, describe, price});
            })
            return arr
        }
    })
    return data
}

exports.electronics = electronics

